package org.itboys.baseobject.service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.itboys.baseobject.entity.KVConfig;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.redis.RedisCacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.mongodb.morphia.query.Query;
/**
 * key-value 配置业务层
 * @author weisky
 *
 */
@Service
public class KVConfigService extends BaseService<KVConfig, String>{
	
	private static final long serialVersionUID = 3934263622122701673L;
	
	private String KEY_VALUE_CONFIG_CACHE = "KVCONFIG_";
	
	@Resource(name="baseMongoDS")
	private MongoDataSource ds;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<KVConfig> getEntityClass() {
		return KVConfig.class;
	}
	
	@Autowired
	private RedisCacheClient redisCacheClient;

	
	public String getValue(String key) {
		String cacheKey = KEY_VALUE_CONFIG_CACHE+key;
		String value = redisCacheClient.get(cacheKey, String.class);
		System.out.println(value);
		if(StringUtils.isNotBlank(value)){
			return value;
		}
		
		KVConfig config = super.getById(key);
		if(config != null && config.getValue() != null) {
			redisCacheClient.set(cacheKey, config.getValue(), -1);
			return config.getValue();
		}
		return null;
	}

	public Integer getIntValue(String key) {
		String value = this.getValue(key);
		if (NumberUtils.isNumber(value)) {
			return Integer.parseInt(value);
		}
		return null;
	}

	public Boolean getBooleanValue(String key) {
		String value = this.getValue(key);
		if ("true".equals(value) || "false".equals(value)) {
			return Boolean.valueOf(value);
		}
		return null;
	}

	public Long getLongValue(String key) {
		String value = this.getValue(key);
		if (NumberUtils.isNumber(value)) {
			return Long.parseLong(value);
		}
		return null;
	}

	/**
	 * 新增或修改
	 * @param key
	 * @param value
	 * @param desc
	 */
	public void saveOrUpdate(String key, String value, String desc) {
		KVConfig kv = new KVConfig();
		kv.setKey(key);
		kv.setValue(value);
		kv.setDesc(desc);
		String cacheKey = KEY_VALUE_CONFIG_CACHE+key;
		redisCacheClient.set(cacheKey, kv, 0);
		super.save(kv);
	}
	
	public List<KVConfig> list(Map<String, Object> param){
		Iterator<String> ite = param.keySet().iterator();
		Query<KVConfig> query = getMongoDataSource().createQuery(getEntityClass());
		while (ite.hasNext()){
			query = prepareQuery(param, ite, query);
		}
		query = query.order(StringUtils.isEmpty((String) param
				.get(ORDER_BY_KEY)) ? "key" : (String) param.get(ORDER_BY_KEY));
		return query.asList();
	}
	
	/**
	 * 删除一个配置，并在缓存中也删除
	 * @param key
	 */
	public void deleteAndCachClean(String key){
		redisCacheClient.del(KEY_VALUE_CONFIG_CACHE + key);//从缓存中删除
		super.delete(key);
	}
}
